import G from "../Utilities/Global";

/**
 * 钥匙
 * created by Five on 2018-11-27 10:36:34
 */
const { ccclass, property } = cc._decorator

@ccclass
export default class FlyingKey extends cc.Component {
    // 飞行的目的地
    @property(cc.Node)
    target: cc.Node = null;
    @property
    speed: number = 200;

    public SetTarget(n: cc.Node, callback: Function) {
        let distance = G().Calc.Distance(this.node.position, n.position);
        let duration = distance / this.speed;
        let lpos = this.node.position;
        let tpos = n.position;
        let x = tpos.x > lpos.x ? 0 : tpos.x;
        let bezier = [cc.v2(x, lpos.y), cc.v2(tpos.x, lpos.y), tpos];
        let aMove = cc.bezierTo(duration, bezier);
        let bRota = cc.rotateTo(duration, 720);
        let spawn = cc.spawn(aMove, bRota);
        let squen = cc.sequence(spawn, cc.callFunc(callback));
        this.node.runAction(squen);
    }
}